"""
fle文件操作-操作系统底层关系-写入文件
编码知识-中文乱码问题解决
关闭流要点1-finally异常管理
关闭流要点2-with上下文管理
文本文件的读取
文本文件操作-为每行添加行号
二进制文件的读写-图片文件的拷贝
文件对象常用方法和属性总结-seek()任意位置操作
使用pickle实现序列化和反序列化
CSV文件的读取和写入
Os模块-调用操作系统可执行文件-控制台乱码问题
os模块-获取文件信息-创建和删除文件夹
os.path模块-常用方法
os模块-使用walk递归遍历所有子目录和子文件
shutil模块文件和目录的拷贝
shutil和zipfile模块-压缩和解压缩
递归算法原理-自己复习前面讲过的算法原理
递归算法-目录树结构的展示
"""
# with open(r"d:\test.txt", "w", encoding="utf-8") as f:
#     # f.write("你好")
#     s = ["年龄\n", "姓名\n", "学校\n"]
#     f.writelines(s)

# # 结合异常机制的finally,确保关闭文件对象
# try:
#     f = open(r"d:\test.txt", "w")
#     s = "hello world"
#     f.write(s)
# except BaseException as e:
#     print(e)
# finally:
#     f.close()

# with open(r"d:\test.txt", "r", encoding="utf-8") as f:
#     while True:
#         line = f.readline()
#         if not line:
#             break
#         else:
#             print(line, end="")

# with open(r"d:\test.txt", "r", encoding="utf-8") as f:
#     lines = f.readlines()
#     print(lines)
#     lines2 = [lines.rstrip() + " #" + str(index) + "\n" for index, lines in zip(range(1, len(lines) + 1), lines)]
#     print(lines2)
#
# with open(r"d:\test.txt", "w", encoding="utf-8") as f:
#     f.writelines(lines2)

# with open(r"src.png", "rb") as srcFile, open(r"dist.png", "wb") as distFile:
#     for line in srcFile:
#         distFile.write(line)

# import pickle
#
# with open("data.dat", "wb") as f:
#     name = 'jack'
#     age = 20
#     sorce = [70, 80, 90]
#     resume = {'name': name, 'age': age, 'sorce': sorce}
#     pickle.dump(resume, f)
#
# with open("data.dat", "rb") as f:
#     resume2 = pickle.load(f)
#     print(resume2)

# import csv
#
# headers = ["年龄", "姓名"]
# rows = [(20, 'jack'), (40, 'john')]
# with open("data.csv", "w") as f:
#     # 创建csv对象
#     b_csv = csv.writer(f)
#     # 写入一行（标题）
#     b_csv.writerow(headers)
#     # 写入多行（数据）
#     b_csv.writerows(rows)
#
# with open("data.csv") as f:
#     # 创建csv对象，他是一个包含所有数据的列表，每一行为一个元素
#     a_csv = csv.reader(f)
#     # 获得列表对象，它包含标题列的信息
#     headers = next(a_csv)
#     # 循环打印各行内容
#     for row in a_csv:
#         print(row)

# import os
#
# # 打印基本信息
# print(os.name)  # windows--nt  linux-->posix
# print(os.sep)  # windows-->\  linux-->/
# print(repr(os.linesep))  # windows-->\r\n  linux-->\n
# a = '3'
# print(a)
# print(repr(a))  # repr可显示数据信息
# # 获取文件和文件夹的相关信息
# print(os.stat("practice.py"))
# # 关于工作目录的操作
# print(os.getcwd())  # 获取当前工作目录
# os.chdir("d:")  # 当前工作目录就变成了d: 的根目录
# # 创建目录，删除目录
# os.mkdir("test")
# os.rmdir("test")
# # 创建多级目录
# os.makedirs("test1/test2/test3")
# # 更改目录名字
# os.rename("test", "测试")
# # 列出子目录
# dirs = os.listdir("test1")
# print(dirs)

# import os.path
#
# path = os.getcwd()
# # 列出子目录和子文件
# file_list = os.listdir(path)
# # 只打印以.py结尾的文件名
# for file_name in file_list:
#     pos = file_name.rfind(".")
#     if file_name[pos + 1:] == "py":
#         print(file_name)
# print("*********************************")
# # 用推导式实现
# file_list2 = [file_name for file_name in os.listdir(path) if file_name.endswith(".py")]
# print(file_list2)

# import os
#
# path = os.getcwd()
# # topdown=False:先遍历子目录再遍历top目录
# list_files = os.walk(path, topdown=False)
# for root, dirs, files in list_files:
#     for name in files:
#         print(os.path.join(root, name))
#     for name in dirs:
#         print(os.path.join(root, name))

# import shutil
# import zipfile
#
# shutil.copyfile("test.txt", "test_copy.tet")
# # 递归拷贝  忽略所有的html,htm文件
# shutil.copytree("source", "destination", ignore=shutil.ignore_patterns("*.html", "*.htm"))
# # 压缩某个文件
# shutil.make_archive("d:/destination", "zip", "d:/source")
# # 压缩：将指定多个文件压缩到一个zip文件
# z = zipfile.ZipFile("test.zip", "w")
# z.write("a.txt")
# z.write("b.txt")
# z.write("c.txt")
# z.close()
# # 解压缩
# z2 = zipfile.ZipFile("test.zip", "r")
# # 解压到的位置
# z2.extractall("d:/")
# z2.close()

import os

# def my_print_file(path, level):
#     child_files = os.listdir(path)
#     for file in child_files:
#         file_path = os.path.join(path, file)
#         print("\t" * level + file_path[file_path.rfind(os.sep) + 1:])
#         if os.path.isdir(file_path):
#             my_print_file(file_path, level + 1)
#
#
# my_print_file("test1", 0)


import pandas as pd

# links  movies  ratings  tags
data = pd.read_csv(r'D:\Movie_Recommend\movies_dataset\tags.csv', encoding='utf-8')
with open('tags.txt', 'w', encoding='utf-8') as f:
    for line in data.values:
        # print(len(line))
        print(line[2])
        # if not pd.isna(line[1]) and not pd.isna(line[2]):
        #     f.write((str(int(line[0])) + ',' + str(int(line[1])) + ',' + str(int(line[2])) + '\n'))
        # elif pd.isna(line[1]) and not pd.isna(line[2]):
        #     f.write((str(int(line[0])) + ',' + str("") + ',' + str(int(line[2])) + '\n'))
        # elif pd.isna(line[2]) and not pd.isna(line[1]):
        #     f.write((str(int(line[0])) + ',' + str(int(line[1])) + ',' + str("") + '\n'))
        # else:
        #     f.write((str(int(line[0])) + ',' + str("") + ',' + str("") + '\n'))

        f.write((str(int(line[0])) + ',' + str(int(line[1])) + ',' + str(line[2]) + ',' + str(int(line[3])) + '\n'))
